#ifndef TimerAwaiter_H
#define TimerAwaiter_H

#include <QTimer>

#include "ccoroutine_qt_global.h"
#include "awaiters.h"
#include "coroutine.h"
namespace CCoroutine{
class TimerAwaiter:public AwaiterBase{
public:
    TimerAwaiter(QTimer* timer):timer(timer){
        QObject::disconnect(con);
    }
    bool ready(){return !timer->isActive();}
    void suspend(PromiseBase* p){
        con = QObject::connect(timer,&QTimer::timeout,[=](){
            QObject::disconnect(con);
            p->resume();
        });
    };
    void resume(){}
private:
    QTimer* timer;
    QMetaObject::Connection con;
};
}
CCoroutine::TimerAwaiter* getAwaiter(QTimer* timer){
    return new CCoroutine::TimerAwaiter(timer);
}
CCoroutine::TimerAwaiter* getAwaiter(QTimer& timer){
    return new CCoroutine::TimerAwaiter(&timer);
}

#endif
